/**
 * @author Jesse McCane
 * @author Berkley Shands
 */

/*
 * This class creates a deck of cards for use with card games
 */
package test.fingers;
import java.util.Random;

public class Deck{
	private int numSuits, numValues, totalCards;
	private Random generator = new Random();
	private Card cardsInDeck[];
	private boolean cardsUsed[];

	Deck(){ //defaults to a typical deck
		this.numSuits = 4;
		this.numValues = 13;
		this.totalCards = 52;
		populateDeck();
	}

	Deck(int suits, int values){ //we can specify the number of cards and suits for the deck if we wanna be crazy
		this.numSuits = suits;
		this.numValues = values;
		this.totalCards = suits * values;
		populateDeck();
	}

	//Fills in the deck with cards
	private void populateDeck(){
		cardsUsed = new boolean[totalCards];
		cardsInDeck = new Card[totalCards];
		int curCard = 0;

		for(int suit = 2; suit <= numSuits + 1; suit++){//adds all the cards to the deck, suits 2-5, values 1-13
			for(int value = 1; value <= numValues; value++){
				cardsInDeck[curCard] = new Card(value, suit);
				cardsUsed[curCard] = false;
				curCard++;
			}
		}
	}

	//deal out a hand of x size
	public Card[] dealHand(int handSize){
		if(handSize > totalCards || handSize < 1){ //keep bad input out
			//System.out.println("Invalid handSize entered: " + handSize);
			//TODO pop a toast up to display error
			return null;
		}

		Card hand[] = new Card[handSize];
		int randomIndex = 0;

		for(int i = 0; i < handSize; i++){
			do{
				randomIndex = generator.nextInt(totalCards);
			}while(cardsUsed[randomIndex]); //do until a new card is found

			cardsUsed[randomIndex] = true; //set card to used
			hand[i] = cardsInDeck[randomIndex]; //add card to hand
		}

		return hand;
	}

	//clear used cards
	public void shuffleCards(){
		for(int i = 0; i < totalCards; i++){
			cardsUsed[i] = false;
		}
	}
}
